package common;

import java.io.Serializable;
import java.sql.Date;
import java.util.List;

import common.History.historyEntry;


public class Profile implements Serializable {
	
    /**
     * 
     */
    private static final long serialVersionUID = -2885167784899658802L;

    final static int initialRating = 1000;
    
	String userName;
	String password;
	History history;
	int rating;
	String email;
	boolean guest;
	boolean dirty;
	int addedHistory;
	
	public Profile(String userName,String password,String email,int rating) {
		this.userName = userName;
		this.password = password;
		this.email = email;
		this.rating = rating;
		history = new History();
		guest = false;
		dirty = false;
		addedHistory = 0;
	}
	
	public Profile(String userName,String password,String email) {
		this(userName,password,email, initialRating);
	}

	public Profile(String guestName) {
		userName = guestName;
		guest = true; 
	}
	
	public boolean isGuest() {
		return guest;
	}
	
	public void setGuest(){
		guest = true;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
		dirty = true;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
		dirty = true;
	}

	public String getUserName() {
		return userName;
	}

	public History getHistory() {
		return history;
	}

	public int getRating() {
		return rating;
	}
	
	public void setRating(int rating) {
		this.rating = rating;
		dirty = true;
	}
	
	public void increaseRating(int amount) {
		rating += amount;
		dirty = true;
	}
	
	public void decreaseRating(int amount) {
		rating -= amount;
		dirty = true;
	}
	
	public boolean isDirty() {
		return dirty;
	}
	
	public History getAddedHistories() {
		History result = new History();
		for (int i=addedHistory;i>0;i--) {
			historyEntry entry = getHistory().getEntry(getHistory().getNumOfHistories()-i);
			result.addEntry(entry);
		}
		return result;
	}
	
	public void resetDirtyBits() {
		dirty = false;
		addedHistory = 0;
	}
	
	public void addHistoryEntry(Date date, List<String> rivals, int score) {
		getHistory().addEntry(date, rivals, score);
		addedHistory++;
	}
	
	public String toString() {
	    return this.getUserName();
	}
}
